package com.aitest.wyjai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;

@RestController
@RequestMapping("/ai")
public class MyAIController {

    @Autowired
    private ChatClient client;

    @RequestMapping("/say")
    public String say(String msg) {
        String content = client.prompt().user(msg).call().content();
        return content;
    }


    @PostMapping(value = "/chat",produces = "text/plain;charset=UTF-8")
    public Flux<String> say2(@RequestParam("prompt")String prompt, String chatId) {
        Flux<String> content = client.prompt().user(prompt)
                .advisors(a -> a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
                .stream().content();
        return content;
    }


    /*@PostMapping(value = "/chat")
    public String say3(String prompt) {
        String content = client.prompt().user(prompt).call().content();
        return content;
    }*/
}
